var app, interval, limit, setTimeout_bool, time_out, appId, zmq_timeout, yk_id, random_id, user_id, tcp_url;

// 获取随机数
function get_random() {
    return parseInt(Math.random() * 100000000);
}

limit = 10;

interval = 90;

appId = void 0;

time_out = 10000;

zmq_timeout = 1000;

random_id = get_random();

setTimeout_bool = function() {
    return setTimeout(function() {
        return false;
    }, time_out);
};

app = angular.module('demo', ['ionic', 'demo.env', 'demo.route', 'ngMessages', 'ngCordova', 'ngStorage', 'starter.baiduMap', 'angular-md5']);


app.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
});

app.factory('myInterceptor', [
    '$injector', '$localStorage', 'ENV', 'version', '$rootScope',
    function($injector, $localStorage, ENV, version, $rootScope) {
        var httpInterceptor;
        httpInterceptor = {
            request: function(config) {
                var auser, login_doctorId;
                if ($localStorage.user) {
                    auser = $localStorage.user.userid;
                    login_doctorId = $localStorage.user.doctorId;
                }
                if (config.url.substr(config.url.length - 5) !== '.html') {
                    config.headers.Authorization = $localStorage.freeapp_token;
                    config.headers.AuthInfo = JSON.stringify({
                        'userId': auser,
                        'appId': ENV.appId,
                        'doctorId': login_doctorId,
                        'platform': ionic.Platform.platform(),
                        'templatesid': 4,
                        'version': version
                    });
                }
                return config;
            },
            response: function(response) {
                var rootScope;
                if (response.config.url.substr(response.config.url.length - 5) !== '.html') {
                    if (response.status === 200) {
                        return response;
                    } else {
                        if (parseInt(response.data.code, 10) === 419) {
                            rootScope = $injector.get('$rootScope');
                            rootScope.$state.go('signin', {
                                type: true
                            });
                            role = 5;
                            $localStorage.user = '';
                            $localStorage.freeapp_token = '';
                            $rootScope.$broadcast('badgeCountNum', 0);
                        }
                        return response;
                    }
                } else {
                    return response;
                }
            }
        };
        return httpInterceptor;
    }
]);

app.config([
    '$ionicConfigProvider', '$httpProvider',
    function($ionicConfigProvider, $httpProvider) {
        $ionicConfigProvider.setPlatformConfig('android', {});
        $ionicConfigProvider.views.transition('none');
        $ionicConfigProvider.views.swipeBackEnabled(false);
        $httpProvider.interceptors.push('myInterceptor');
    }
]);